﻿using EF6PowerToolDemo.Models1;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EF6PowerToolDemo
{
    /// <summary>
    /// 《Entity Framework 6 Recipes》中文翻译系列
    /// http://www.cnblogs.com/VolcanoCloud/p/4517310.html
    /// </summary>
    public class EFRecipesEntities : DbContext
    {
        static EFRecipesEntities()
        {
            //Database.SetInitializer( new CreateDatabaseIfNotExists<EFRecipesEntities>()); //默认行为
            //Database.SetInitializer(new DropCreateDatabaseAlways<EFRecipesEntities>());
            //Database.SetInitializer<EFRecipesEntities>(null);
            Database.SetInitializer(new MigrateDatabaseToLatestVersion<EFRecipesEntities,
                Migrations.Configuration>());
        }

        public EFRecipesEntities() 
            : base("EFRecipesEntities")
        {
        }

        public DbSet<Associate> Associates { get; set; }
        public DbSet<AssociateSalary> AssociateSalaries { get; set; }
        public DbSet<Payment> Payments { get; set; }

        public DbSet<Product> Products { get; set; }



        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            //
            modelBuilder.Entity<AssociateSalary>().HasKey(x => x.SalaryId);
            modelBuilder.Entity<AssociateSalary>().ToTable("Chapter3.AssociateSalary");
            //
            modelBuilder.Entity<Associate>().ToTable("Chapter3.Associate");
            //
            modelBuilder.Entity<Payment>().ToTable("Chapter3.Payment");
            //
            modelBuilder.Entity<Product>().Map(m =>
            {
                m.Properties(p => new { p.SKU, p.Description, p.Price });
                m.ToTable("Product", "Chapter2");
            })
            .Map(m =>
            {
                m.Properties(p => new { p.SKU, p.ImageURL });
                m.ToTable("ProductWebInfo", "Chapter2");
            });


       
        }
    }
}
